package com.dmooo.yxmmerchant.http;

import com.common.net.entity.BaseEntity;
import com.common.net.listener.ProgressListener;
import com.common.net.util.MyGsonConverter;
import com.common.net.util.UploadFileRequestBody;
import com.dmooo.yxmmerchant.common.Config;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * http交互处理类
 * Created by WMM on 2019/6/12.
 */
public class HttpManager {
    /**
     * 设置超时时间，默认10s
     */
    private static final int DEFAULT_TIMEOUT = 10;
    private HttpService httpService;
    private volatile static HttpManager INSTANCE;
    private BaseEntity baseBar;
    private Observable observable;

    //构造方法私有
    private HttpManager() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        builder.connectTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        builder.readTimeout(DEFAULT_TIMEOUT, TimeUnit.SECONDS);
        builder.addInterceptor(new CommonParamInterceptor());
        Retrofit retrofit = new Retrofit.Builder()
                .client(builder.build())
                .addConverterFactory(MyGsonConverter.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(Config.APP_INTERFACE_BASE_URL)
                .build();
        httpService = retrofit.create(HttpService.class);
    }

    //获取单例
    public static HttpManager getInstance() {
        if (INSTANCE == null) {
            synchronized (HttpManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new HttpManager();
                }
            }
        }
        return INSTANCE;
    }

    public void userLogin(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.userLogin(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void loginAdmin(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.loginAdmin(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void getArticleListByCatId(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getArticleListByCatId(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void userRegiter(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.userRegiter(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void getServiceDetail(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getServiceDetail(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void sendUserRegister(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.sendUserRegister(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    } public void getMsg(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getMsg(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void version(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.version(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    } public void getOrderList2(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getOrderList2(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }  public void getServiceOrderList2(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getServiceOrderList2(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    } public void statisticsOrder(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.statisticsOrder(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }public void statisticsServiceOrder(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.statisticsServiceOrder(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void getGoodsMsg(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getGoodsMsg(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void getMerchantMsg(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getMerchantMsg(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void editMerchantMsg(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.editMerchantMsg(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void statistics(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.statistics(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void getArticleMsg(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getArticleMsg(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void suggest(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.suggest(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void sendEmail(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.sendEmail(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void bandingEmail(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.bandingEmail(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void editUserMsg(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.editUserMsg(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void findPwdByPhone(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.findPwdByPhone(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void sendUserFindpwd(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.sendUserFindpwd(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void changePwd(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.changePwd(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void getBannerList(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getBannerList(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void delBanner(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.delBanner(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void getBankTypeList(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getBankTypeList(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void getUserBank(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getUserBank(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void addUserBank(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.addUserBank(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void delUserBank(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.delUserBank(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void sendDrawPwdCode(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.sendDrawPwdCode(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void setDrawPwd(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.setDrawPwd(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void userDrawMoney(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.userDrawMoney(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void isSetDrawPwd(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.isSetDrawPwd(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void checkDrawPwd(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.checkDrawPwd(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void getGoodsOrderList(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getGoodsOrderList(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void getLogisticsList(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getLogisticsList(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void getLogisticsMsg(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getLogisticsMsg(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void statisticsByService(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.statisticsByService(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void statisticsByGoods(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.statisticsByGoods(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void getDrawRecord(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getDrawRecord(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void getBalanceRecord(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getBalanceRecord(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void checkRefund(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.checkRefund(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void refund(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.refund(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void confirmPay(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.confirmPay(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void confirmServicePay(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.confirmServicePay(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void finishService(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.finishService(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void serviceCheckRefund(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.serviceCheckRefund(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void sendGoodsOrder(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.sendGoodsOrder(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void getGoodsOrderDetail(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getGoodsOrderDetail(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void getServiceOrderDetail(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getServiceOrderDetail(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void confirmServiceOrderPay(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.confirmServiceOrderPay(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void getServiceOrderList(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getServiceOrderList(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void getImgList(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getImgList(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void delAlbImg(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.delAlbImg(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void edtiNotice(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.edtiNotice(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void getNotice(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getNotice(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void getGoodsAuth(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getGoodsAuth(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void getServiceAuth(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getServiceAuth(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void getPartialPay(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getPartialPay(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void setPartialPay(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.setPartialPay(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void getServiceAdminList(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getServiceAdminList(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void delPartialPay(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.delPartialPay(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void getAdminList(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getAdminList(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }  public void treatInvoice(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.treatInvoice(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void changeServiceOrderPrice(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.changeServiceOrderPrice(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void getInvoiceApply(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getInvoiceApply(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void changeOrderPrice(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.changeOrderPrice(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void getAdminMsg(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getAdminMsg(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    public void addAdmin(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.addAdmin(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void delAdmin(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.delAdmin(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void editAdmin(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.editAdmin(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void changeBandingPhone(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.changeBandingPhone(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void sendChangeBandCode(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.sendChangeBandCode(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void sendChangeBanding(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.sendChangeBanding(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void addCoupon(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.addCoupon(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void getCouponList(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.getCouponList(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void delCoupon(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.delCoupon(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    public void editCoupon(BaseEntity basePar) {
        baseBar = basePar;
        observable = httpService.editCoupon(basePar.getParams()).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }
    /**
     * @param basePar
     * @param listener
     */
    public void upUserAvatar(BaseEntity basePar, ProgressListener listener, String token) {
        baseBar = basePar;
        Map<String, RequestBody> map = new HashMap<>();
        UploadFileRequestBody fileRequestBody = new UploadFileRequestBody(basePar.getFile(), listener);
        map.put("user_avatar\"; filename=\"" + basePar.getFile().getName() + "", fileRequestBody);
        observable = httpService.upUserAvatar(map, token).map(basePar);
        toSubscribeOn(observable, basePar.getSubscirber());
    }

    /**
     * 请求响应订阅
     *
     * @param observer
     * @param subscriber
     */
    private void toSubscribeOn(Observable observer, Subscriber subscriber) {
        observer.subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber);
    }

    /**
     * 取消请求
     */
    public void cancle() {
        if (baseBar != null && observable != null) {
            if (!baseBar.getSubscirber().isUnsubscribed()) {
                baseBar.getSubscirber().unsubscribe();
            }
            baseBar = null;
            observable = null;
        }
    }
}
